﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EQual.MainModule.Employees.Trainings
{
    public class TrainingParticipantPresenter : EQual.Infrastructure.MVPFramework.Presenters.ListPresenterDefault<Employees .IEmployeesSelectionView , object>
    {

        public TrainingParticipantPresenter()
        {
            View = new Employees.EmployeesSelectionView();
            View.ItemAdded += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemAdded);
        }

        void View_ItemAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
              new DataAccesLayer.EQualDataLayerFactory().GetEmployeeAccesLayer ().AddEmployeeTrainings (ActiveItem , e.Value);
        }

        public override void Dispaly(object item)
        {
            TrainingPresentationModel ViewModel = new TrainingPresentationModel  ();
            ViewModel .Training  = item ;
            View.Model = ViewModel;
            ViewModel.ModelCollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ViewModel_ModelCollectionChanged);
        }

        void ViewModel_ModelCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {

                new DataAccesLayer.EQualDataLayerFactory().GetEmployeeAccesLayer().RemoveEmployeeTrainings(ActiveItem, e.OldItems);
            }
        }

    }

    public class TrainingPresentationModel : Employees.Models.EmployeesPresentationModel
    {
        object training;

        public object Training
        {
            get { return training; }
            set { training = value; }
        }

        protected override IEnumerable<DataAccesLayer.Employee> GetCollection()
        {
            return new DataAccesLayer.EQualDataLayerFactory().GetEmployeeAccesLayer().GetTrainingParticipants(Training) as IEnumerable <DataAccesLayer .Employee >;
        }

        protected override void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
           
            base.CollectionChanged(sender, e);
        }
    }

}
